#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class Web_NewsProvider : CY.GFive.Core.Providers.DALProvider.IWeb_NewsProvider
	{
		#region Sql
		private static readonly string SqlSelectWeb_News = "SELECT [Id], [News_Title],[News_Content],[News_PubDate],[Module_Code],[Manager_Code],[IsCheck] FROM [Web_News] WHERE  [Id]=@Id";

		private static readonly string SqlInsertWeb_News = "INSERT INTO [Web_News]([News_Title],[News_Content],[News_PubDate],[Module_Code],[Manager_Code],[IsCheck]) values(@News_Title,@News_Content,@News_PubDate,@Module_Code,@Manager_Code,@IsCheck);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateWeb_News = "UPDATE [Web_News] SET [News_Title] = @News_Title,[News_Content] = @News_Content,[News_PubDate] = @News_PubDate,[Module_Code] = @Module_Code,[Manager_Code] = @Manager_Code,[IsCheck] = @IsCheck WHERE  [Id]=@Id";

		private static readonly string SqlDeleteWeb_News = "DELETE [Web_News] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllWeb_News = "SELECT [Id], [News_Title],[News_Content],[News_PubDate],[Module_Code],[Manager_Code],[IsCheck] FROM [Web_News]";
		#endregion

		#region IWeb_NewsProvider Members
		public Core.Business.Web_News Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectWeb_News);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Web_News web_News = new Core.Business.Web_News();

				if (!reader.IsDBNull(0)) web_News.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) web_News.News_Title = reader.GetString(1);
				if (!reader.IsDBNull(2)) web_News.News_Content = reader.GetString(2);
				if (!reader.IsDBNull(3)) web_News.News_PubDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) web_News.Module_Code = reader.GetString(4);
				if (!reader.IsDBNull(5)) web_News.Manager_Code = reader.GetString(5);
				if (!reader.IsDBNull(6)) web_News.IsCheck = reader.GetBoolean(6);

				return web_News;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Web_News web_News)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@News_Title", SqlDbType.NVarChar, web_News.News_Title);
			sql.AddParameter("@News_Content", SqlDbType.NVarChar, web_News.News_Content);
			sql.AddParameter("@News_PubDate", SqlDbType.DateTime, web_News.News_PubDate);
			sql.AddParameter("@Module_Code", SqlDbType.NVarChar, web_News.Module_Code);
			sql.AddParameter("@Manager_Code", SqlDbType.NVarChar, web_News.Manager_Code);
			sql.AddParameter("@IsCheck", SqlDbType.Bit, web_News.IsCheck);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertWeb_News);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) web_News.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Web_News web_News)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@News_Title", SqlDbType.NVarChar, web_News.News_Title);
			sql.AddParameter("@News_Content", SqlDbType.NVarChar, web_News.News_Content);
			sql.AddParameter("@News_PubDate", SqlDbType.DateTime, web_News.News_PubDate);
			sql.AddParameter("@Module_Code", SqlDbType.NVarChar, web_News.Module_Code);
			sql.AddParameter("@Manager_Code", SqlDbType.NVarChar, web_News.Manager_Code);
			sql.AddParameter("@IsCheck", SqlDbType.Bit, web_News.IsCheck);
			sql.AddParameter("@Id", SqlDbType.Int, web_News.Id);
			sql.ExecuteSql(SqlUpdateWeb_News);
		}

		public void Delete(Core.Business.Web_News web_News)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, web_News.Id);
			sql.ExecuteSql(SqlDeleteWeb_News);
		}

		public IList<Core.Business.Web_News> GetAllWeb_News()
		{
			IList<Core.Business.Web_News> web_Newslist = new List<Core.Business.Web_News>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllWeb_News);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Web_News web_News = new Core.Business.Web_News();

					if (!reader.IsDBNull(0)) web_News.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) web_News.News_Title = reader.GetString(1);
					if (!reader.IsDBNull(2)) web_News.News_Content = reader.GetString(2);
					if (!reader.IsDBNull(3)) web_News.News_PubDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) web_News.Module_Code = reader.GetString(4);
					if (!reader.IsDBNull(5)) web_News.Manager_Code = reader.GetString(5);
					if (!reader.IsDBNull(6)) web_News.IsCheck = reader.GetBoolean(6);

					web_News.MarkOld();
					web_Newslist.Add(web_News);
				}
				reader.Close();
			}
			return web_Newslist;
		}
		#endregion
	}
}
